﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Arcanea.StateEditor.XmlObjects;
using Arcanea.Mediator.Toolbox;
using Arcanea.Mediator.XmlClasses;
using System.IO;

namespace Arcanea.StateEditor.FormOption
{
    public partial class FormOptionLinkRepresentation : Form
    {
        public readonly string xmlFilePathOfPacket = Path.Combine(Application.StartupPath, "Packets.xml");

        private bool OnSetup = false;

        private LinkRepresentationXml modele = new LinkRepresentationXml();
        public LinkRepresentationXml Modele
        {
            get
            {
                return modele;
            }
            set
            {
                modele = value;
                comboBoxFather.Items.Clear();
                comboBoxFather.Items.Add(modele.NamePere);
                comboBoxFather.Items.Add(modele.NameFils);
                comboBoxFather.SelectedIndex = 0;
                radioButtonClientSending.Checked = modele.IsClientDeclenche;
                radioButton1.Checked = !radioButtonClientSending.Checked;

                SelectFromModel();
            }
        }

        private IList<XmlPacket> allXmlPacket = new List<XmlPacket>();
        public IList<XmlPacket> AllXmlPacket
        {
            get
            {
                return allXmlPacket;
            }
            set
            {
                listBoxPacketSelection.Items.Clear();
                allXmlPacket = value;
                foreach(XmlPacket p in allXmlPacket)
                {
                    listBoxPacketSelection.Items.Add(p.Name);
                }
                SelectFromModel();
            }
        }

        private void SelectFromModel()
        {
            OnSetup = true;
            listBoxPacketSelection.SelectedIndices.Clear();
            for (int i = 0; i < listBoxPacketSelection.Items.Count; i++)
            {
                if (modele.AllPacketAdmited.Contains(listBoxPacketSelection.Items[i].ToString()))
                {
                    listBoxPacketSelection.SelectedIndices.Add(i);
                }
            }
            OnSetup = false;
        }

        public FormOptionLinkRepresentation()
        {
            InitializeComponent();
        }

        public FormOptionLinkRepresentation(string XMLFilePathPacket)
            : this()
        {
            AllXmlPacket = XMLToolbox.XmlListPacketLoading(XMLFilePathPacket);
        }

        private void buttonAccept_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void listBoxPacketSelection_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!OnSetup)
            {
                modele.AllPacketAdmited = new List<string>();

                for (int i = 0; i < listBoxPacketSelection.SelectedItems.Count; i++)
                {
                    modele.AllPacketAdmited.Add(listBoxPacketSelection.SelectedItems[i].ToString());
                }
            }
        }

        private void comboBoxFather_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxFather.SelectedIndex == 0)
            {
                labelTextChildren.Text = comboBoxFather.Items[1].ToString();
                if (labelTextChildren.Text != this.modele.NameFils)
                {
                    this.modele.NameFils = labelTextChildren.Text;
                    this.modele.NamePere = comboBoxFather.SelectedItem.ToString();
                    int temp = this.modele.GUIDFils;
                    this.modele.GUIDFils = this.modele.GUIDPere;
                    this.modele.GUIDPere = temp;
                }
            }
            else if (comboBoxFather.SelectedIndex == 1)
            {
                labelTextChildren.Text = comboBoxFather.Items[0].ToString();
                if (labelTextChildren.Text != this.modele.NameFils)
                {
                    this.modele.NameFils = labelTextChildren.Text;
                    this.modele.NamePere = comboBoxFather.SelectedItem.ToString();
                    int temp = this.modele.GUIDFils;
                    this.modele.GUIDFils = this.modele.GUIDPere;
                    this.modele.GUIDPere = temp;
                }
            }
        }

        private void radioButtonClientSending_CheckedChanged(object sender, EventArgs e)
        {
            modele.IsClientDeclenche = radioButtonClientSending.Checked;
        }
    }
}
